﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Shapes;
using Zhy.DigitaPlatform.Components;

namespace Zhy.DigitaPlatform.Common.Converter
{
    /// <summary>
    /// 根据类型字符串反射出一个实例化好的组态控件
    /// 删除命令 DeleteCommand
    /// 删除命令的参数  DeleteParameter
    /// 选中状态  IsSelected
    /// </summary>
    public class DeviceItemConverter : IValueConverter
    {
        /// <summary>
        /// 根据类型字符串反射出一个实例化好的组态控件
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            #region 组件对齐辅助线
            if (value.ToString() == "HL")
            {
                return new Line // 实例化辅助线对象
                {
                    X1 = 0,
                    Y1 = 0,
                    X2 = 2000,
                    Y2 = 0,
                    //Height = 0.5,
                    Stroke = Brushes.Red,
                    StrokeThickness = 1,
                    StrokeDashArray = new DoubleCollection { 3.0, 3.0 },
                    ClipToBounds = true,
                };
            }
            else if (value.ToString() == "VL")
            {
                return new Line
                {
                    X1 = 0,
                    Y1 = 0,
                    X2 = 0,
                    Y2 = 2000,
                    //Width = 0.5,
                    Stroke = Brushes.Red,
                    StrokeThickness = 1,
                    StrokeDashArray = new DoubleCollection { 3.0, 3.0 },
                    ClipToBounds = true,
                };
            }
            #endregion

            var assembly = Assembly.Load("Zhy.DigitaPlatform.Components");
            Type t = assembly.GetType("Zhy.DigitaPlatform.Components." + value.ToString())!;

            // 将组件强转成组件对象父类
            var obj = Activator.CreateInstance(t)!;
            if (new string[] { "WidthRule", "HeightRule" }.Contains(value.ToString()))
                return obj; // 如果是宽度或者高度对齐辅助线，不需要转换对象，直接返回


            // 将其它组件强转成组件对象父类
            var c = (ComponentBase)obj; 

            Binding binding = new Binding(); // 实例化绑定对象
            // 实例化一个ViewModel中的删除命令
            binding.Path = new System.Windows.PropertyPath("DeleteCommand");
            // 将ViewModel中的删除命令绑定到依赖属性上
            c.SetBinding(ComponentBase.DeleteCommandProperty, binding);
            binding = new Binding();
            //binding.Path = new System.Windows.PropertyPath("."); // 把当前对象传进去 - 这部可以省略 - 默认传的就是当前对象
            // 将ViewModel项绑定到参数依赖属性上。
            c.SetBinding(ComponentBase.DeleteParameterProperty, binding); // 删除命令参数绑定

            binding = new Binding();
            // 实例化ViewModel中的选中属性
            binding.Path = new System.Windows.PropertyPath("IsSelected");
            // 将ViewModel中的选中属性绑定到组件的选中依赖属性上
            c.SetBinding(ComponentBase.IsSelectedProperty, binding);

            // 将依赖属性的宽度绑定到ViewModel上
            //binding = new Binding();
            //binding.Path = new System.Windows.PropertyPath("Width");//这里指定的模型中的宽度属性
            //binding.Mode = BindingMode.TwoWay;// 设置双向绑定
            //c.SetBinding(ComponentBase.ShowWidthProperty, binding);//这里指定对象的依赖属性
            //// 将依赖属性的高度绑定到ViewModel上
            //binding = new Binding();
            //binding.Path = new System.Windows.PropertyPath("Height"); //这里指定的模型中的高度属性
            //binding.Mode = BindingMode.TwoWay;// 设置双向绑定
            //c.SetBinding(ComponentBase.ShowHeightProperty, binding);//这里指定对象的依赖属性

            // 处理组件尺寸缩放命令逻辑绑定
            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("ResizeDownCommand"); // 绑定鼠标按下命令
            c.SetBinding(ComponentBase.ResizeDownCommandProperty, binding);

            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("ResizeMoveCommand"); // 绑定鼠标移动命令
            c.SetBinding(ComponentBase.ResizeMoveCommandProperty, binding);

            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("ResizeUpCommand"); // 绑定鼠标抬起命令
            c.SetBinding(ComponentBase.ResizeUpCommandProperty, binding);
            
            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("Rotate"); // 绑定设置旋转角度命令
            c.SetBinding(ComponentBase.RotateAngleProperty, binding);

            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("FlowDirection"); // 绑定设置流向命令
            c.SetBinding(ComponentBase.FlowDirectionProperty, binding);

            // 报警的依赖属性绑定
            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("IsWarning");// Model中的属性
            c.SetBinding(ComponentBase.IsWarningProperty, binding);// 组件中的依赖属性
            // 报警信息的依赖属性绑定
            binding = new Binding();
            binding.Path = new System.Windows.PropertyPath("WarningMessage");// Model中的属性
            c.SetBinding(ComponentBase.WarningMessageProperty, binding);// 组件中的依赖属性

            return c;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
